package cn.good.yan.service.impl;

import cn.good.yan.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 消息队列 - 发送消息的接口
 *
 * @author shengren.yan
 * @create 2024-01-09
 */
// 是一个消息的发送管道的定义
@EnableBinding(Source.class)
public class MessageProviderImpl implements IMessageProvider {

    /**
     * 消息的发送管道
     */
    @Resource
    private MessageChannel output;

    /**
     * 生产者 - 发送消息
     * @return {@link String}
     */
    @Override
    public String send() {
        String serial = "生产者:" + UUID.randomUUID().toString();
        // 创建并发送消息
        this.output.send(MessageBuilder.withPayload(serial).build());
        System.out.println("xxxxx: " + serial);
        return serial;
    }

}
